package com.enqualcomm.kids.mvp.voiceschedule;

import android.media.AmrInputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by yinzhiqun on 2016/3/15.
 */
public class Pcm2Amr {

    public static String convert(String pcmFileName){
        InputStream in;
        OutputStream out;
        try {
            String amrFileName = pcmFileName.replace(".pcm", ".amr");
            in = new FileInputStream(pcmFileName);
            AmrInputStream aStream = new AmrInputStream(in);

            File file = new File(amrFileName);
            file.createNewFile();
            out = new FileOutputStream(file);

            byte[] x = new byte[1024];
            int len;
            out.write(0x23);
            out.write(0x21);
            out.write(0x41);
            out.write(0x4D);
            out.write(0x52);
            out.write(0x0A);
            while ((len = aStream.read(x)) > 0)
            {
                out.write(x, 0, len);
            }

            out.close();
            aStream.close();

            return amrFileName;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

}
